﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using HellCook.Model;

namespace HellCook.WinApp_WPF.RecipeManager
{
    /// <summary>
    /// Interaction logic for AddIngredientQuantity.xaml
    /// </summary>
    public partial class AddIngredientQuantity : Window
    {
        Collection<NodoCantidadIngrediente> ListIngredient;
        DataGrid ListData;

        public AddIngredientQuantity()
        {
            InitializeComponent();
        }

        public AddIngredientQuantity(Collection<NodoCantidadIngrediente> listIngredient, DataGrid listData)
        {
            InitializeComponent();

            this.ListIngredient = listIngredient;
            this.ListData = listData;
        }

        private void button_Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button_Add_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Ingrediente Searched = HellCook.Business.NodoCantidadIngrediente_Registro.BuscarIngrediente(this.textBox_NameIngredient.Text);
                if (Searched == null)
                {
                    MessageBoxResult result;
                    result = MessageBox.Show("El ingrediente no existe, ¿desea agregarlo?", "Mensaje", MessageBoxButton.YesNo);

                    if (result == MessageBoxResult.No)
                    {
                        this.Close();
                    }
                    else
                    {
                        HellCook.WinApp_WPF.AddNewIngredient NuevoIngrediente = new AddNewIngredient(this.textBox_NameIngredient.Text);
                        NuevoIngrediente.ShowDialog();                        
                    }
                }
                else
                {
                    NodoCantidadIngrediente ToAdd = HellCook.Business.NodoCantidadIngrediente_Registro.DameElResultadoDelNuevoIngrediente(Searched, this.textBox_Quantity.Text);
                    if (ToAdd == null)
                    {
                        MessageBoxResult result;
                        result = MessageBox.Show("Error, ¿continuar?", "Mensaje", MessageBoxButton.YesNo);
                        if (result == MessageBoxResult.No)
                        {
                            this.Close();
                        }
                    }
                    else
                    {
                        bool Salir = false;
                        for (int PunteroLista = 0; PunteroLista < ListIngredient.Count; PunteroLista++)
                        {
                            if (ListIngredient[PunteroLista].Codigo == ToAdd.Codigo)
                            {
                                Salir = true;
                            }
                        }

                        if (Salir == false)
                        {
                            this.ListIngredient.Add(ToAdd);
                            this.ListData.ItemsSource = null;
                            this.ListData.ItemsSource = ListIngredient;

                            MessageBoxResult result;
                            result = MessageBox.Show("Agregado a la lista. :-) -> ¿desea continuar?", "mensaje", MessageBoxButton.YesNo);
                            if (result == MessageBoxResult.No)
                            {
                                this.Close();
                            }
                        }
                        else
                        {
                            MessageBoxResult result;
                            result = MessageBox.Show("El ingrediente ya existe en la lista, ¿desea continuar?", "Mensaje", MessageBoxButton.YesNo);
                            if (result == MessageBoxResult.No)
                            {
                                this.Close();
                            }

                        }
                        this.textBox_NameIngredient.Text = "";
                        this.textBox_Quantity.Text = "";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DataGrid_ListaIngredientes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.DataGrid_ListaIngredientes.SelectedIndex >= 0)
            {
                Ingrediente Auxiliar = (Ingrediente)this.DataGrid_ListaIngredientes.SelectedItem;
                this.textBox_NameIngredient.Text = Auxiliar.Nombre_Ingrediente;
                this.Label_TipoMedida.Content = Auxiliar.Tipo_Medida;       
            }
        }

        private void textBox_NameIngredient_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.DataGrid_ListaIngredientes.ItemsSource = HellCook.Business.Ingrediente_Buscador.ObtenerTodosLosIngredientesSimilarA(this.textBox_NameIngredient.Text);

            if (this.DataGrid_ListaIngredientes.Items.Count > 0)
            {
                if (this.DataGrid_ListaIngredientes.Items.Count == 1 &&
                    String.IsNullOrEmpty(this.Label_TipoMedida.Content.ToString()))
                {
                    Ingrediente Auxiliar = (Ingrediente)this.DataGrid_ListaIngredientes.Items[0];                    
                    this.Label_TipoMedida.Content = Auxiliar.Tipo_Medida;
                }
                else
                {
                    if (this.DataGrid_ListaIngredientes.Items.Count > 1)
                    {
                        Ingrediente Auxiliar = (Ingrediente)this.DataGrid_ListaIngredientes.Items[0];
                        this.Label_TipoMedida.Content = Auxiliar.Tipo_Medida;
                    }
                    else
                    {
                        this.Label_TipoMedida.Content = "";
                    }
                }
            }
        }
    }
}
